<template>
  <basic-dialog
    :ok-text="'提交'"
    :width="'40%'"
    :visible.sync="visible"
    :title="'取回'"
    :handleClose="handleCloseDialog"
    :loadingBtn="submitLoading"
    :handle-ok="handleSubmit"
    :handle-open="handleOpen"
  >
    <el-form
      ref="submitForm"
      :model="submitForm"
      :rules="formValidate"
      label-width="90px"
    >
      <el-form-item label="计划编号">
        <el-input v-model="submitForm.planCode" readonly />
      </el-form-item>
      <el-form-item label="供应商名称">
        <el-input v-model="submitForm.supplierName" readonly />
      </el-form-item>
      <el-form-item label="取回原因" prop="cancelPlanReason">
        <el-input show-word-limit v-model.trim="submitForm.cancelPlanReason" maxlength="200" type="textarea" :rows="6" />
      </el-form-item>
    </el-form>
  </basic-dialog>
</template>

<script>
import BasicDialog from '@/components/BasicDialog';
import {
  retrieveWorkflow
} from '@/api/supplier/workflow';
import { isResponseSuccess } from '@/pages/supplier/utils/bizUtils';

export default {
  components: {
    BasicDialog,
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    planData: {
      type: Object,
    },
  },
  data () {
    return {
      submitLoading: false,
      submitForm: {
        planCode: '',
        supplierName: '',
        cancelPlanReason: '',
      },
      formValidate: {
        cancelPlanReason: [
          { required: true, message: '请输入取消原因', trigger: 'blur' },
          { type: 'string', min: 1, max: 200, message: '最多输入200字', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    // 提交
    handleSubmit () {
      this.$refs['submitForm'].validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          retrieveWorkflow(this.submitForm).then(res => {
            this.submitLoading = false;
            if (isResponseSuccess(res)) {
              this.$message.success('提交成功')
              this.$emit('getTableData')
              this.handleCloseDialog()
            }
          })
        }
      })
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:visible', false);
    },
    handleOpen () {
      this.submitForm = {
        cancelPlanReason: '', // 说明
        ...this.planData
      }
    }
  }
}
</script>
